package com.xgh.xiaohui;

/**
 * @ClassName CharAlgo
 * @Description
 * @Author xinggh
 * @Date 2020/7/16 12:55
 * @Version 1.0
 **/
public class CharAlgo {
    /**
     * 什么是字符串匹配算法？
     * s1 主串，s2 匹配串
     */
    public static int getIndex(String s1, String s2) {

        //1 暴力算法
        int l1 = s1.length();
        int l2 = s2.length();
        if (l1 < l2) {
            return -1;
        }
        int j = 0;
        for (int i = 0; i < l1; i++) {
            int n = i;
            while (n < l1) {
                if (s1.charAt(n) == s2.charAt(j)) {
                    n++;
                    j++;
                    if (j == l2) {
                        return i;
                    }
                } else {
                    j = 0;
                    break;
                }
            }
        }

        return -1;
    }

    public static void main(String[] args) {
        String s1 = "bbbce";
        String s2 = "bce";
        System.out.println(getIndex(s1, s2));
    }


}
